package com.tiger.cloud.controller.crm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tiger.cloud.common.mybatis.BaseController;
import com.tiger.cloud.enums.DeleteEnum;
import com.tiger.cloud.filter.CurrentUserId;
import com.tiger.cloud.filter.IgnoreAuth;
import com.tiger.cloud.form.SysDictSmall;
import com.tiger.cloud.model.crm.UserCompanyInfo;
import com.tiger.cloud.service.crm.IUserCompanyInfoService;
import com.tiger.cloud.service.system.CommonDict;
import com.tiger.cloud.util.ErrorCode;
import com.tiger.cloud.util.Response;
import com.tiger.cloud.util.ResponseUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 用户企业信息
 */
@Controller
@RequestMapping(value = "userCompanyInfo")
@Api(value = "用户企业信息管理",tags = "用户企业信息")
public class UserCompanyInfoController extends BaseController {
	
	@Autowired
	private IUserCompanyInfoService userCompanyInfoService;
	
	/**
	 * 获取列表默认值
	 */
	@IgnoreAuth
	@RequestMapping(value = "getDefaultFileds", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "获取默认用户企业信息",notes = "获取用户企业信息")
	public Response getDefaultFileds(){
		Map<String, List<SysDictSmall>> values = CommonDict.getFileds("user_company_info");
		return ResponseUtil.ok(values);
	}
	
	@RequestMapping(value = "get", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "获取用户企业信息列表",notes = "获取用户企业信息列表,列如在修改时候调取")
	public Response get(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId){
		UserCompanyInfo userCompanyInfo=new UserCompanyInfo();
		userCompanyInfo.setIsDelete(DeleteEnum.NOMAIL);
		userCompanyInfo.setUserId(userId);
		List<UserCompanyInfo> userCompanyInfos=userCompanyInfoService.find(userCompanyInfo);
		Map<String, List<SysDictSmall>> values = CommonDict.getFileds("user_company_info");
		Map<String, Object> maps=new HashMap<String, Object>();
		maps.put("userCompanyInfo", userCompanyInfos);
		maps.put("defaults", values);
		return ResponseUtil.ok(maps);
	}
	
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "更新用户企业信息列表",notes = "更新用户企业信息列表,列如在修改时候调取")
	public Response update(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,@RequestBody UserCompanyInfo userCompanyInfo){
		if(userCompanyInfo.getId()==null){
			return ResponseUtil.error(ErrorCode.PARAMETER_INVALID,"id必传!");
		}
		if(userCompanyInfo.getUserId()==null){
			return ResponseUtil.error(ErrorCode.PARAMETER_INVALID,"userId必传!");
		}
		if(userCompanyInfo.getId()!=null&&userCompanyInfo.getId()>0){
			userCompanyInfo.setUserId(userId);
			UserCompanyInfo updateEntity=userCompanyInfoService.get(userCompanyInfo.getId());
			if(updateEntity==null||updateEntity.getUserId()!=userId){
				return ResponseUtil.error("请不要更新不是自己的企业数据!");
			}
			if(userCompanyInfoService.updateCompanyInfo(userCompanyInfo)){
				return ResponseUtil.ok();
			};
			return ResponseUtil.error();
		}
		return ResponseUtil.error("参数错误,请重试!");
	}
	
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "添加用户企业信息列表",notes = "添加用户企业信息列表")
	public Response add(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,@RequestBody UserCompanyInfo userCompanyInfo){
		Map<String, Object> query=new HashMap<String, Object>();
		long count=userCompanyInfoService.count(query);
		userCompanyInfo.setCompanyNo((int)(count+1));
		userCompanyInfo.setUserId(userId);
		userCompanyInfo.setIsDelete(DeleteEnum.NOMAIL);
		userCompanyInfo.setUserId(userId);
		if(userCompanyInfoService.insertUserCompany(userCompanyInfo)){
			return ResponseUtil.ok();
		}
		return ResponseUtil.error();
	}

	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "删除用户企业信息列表",notes = "删除用户企业信息列表")
	public Response delete(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,@RequestBody UserCompanyInfo userCompanyInfo){
		if(userCompanyInfo.getId()!=null&&userCompanyInfo.getId()>0){
			UserCompanyInfo deleteEntity=userCompanyInfoService.get(userCompanyInfo.getId());
			if(deleteEntity==null||deleteEntity.getUserId()!=userId){
				return ResponseUtil.error("参数错误,请重试!");
			}
			deleteEntity.setIsDelete(DeleteEnum.DELETE);
			if(userCompanyInfoService.update(deleteEntity)>0){
				return ResponseUtil.ok();
			};
			return ResponseUtil.error();
		}
		return ResponseUtil.error("参数错误,请重试!");
	}

}